package com.gk.service.impl;

import com.alibaba.fastjson.JSON;
import com.gk.bean.CommonDto;
import com.gk.bean.T101HeaderBean;
import com.gk.bean.T102HeaderBean;
import com.gk.bean.T103HeaderBean;
import com.gk.bean.qfdto.BtypeAddressDto;
import com.gk.bean.qfdto.BtypeDto;
import com.gk.bean.qfdto.PtypeDto;
import com.gk.dao.BtypeDao;
import com.gk.dao.PtypeDao;
import com.gk.http.HttpApi;
import com.gk.http.entity.HttpCommon;
import com.gk.http.entity.HttpRequestParams;
import com.gk.http.entity.HttpResult;
import com.gk.service.BaseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BaseInfoServiceImpl implements BaseInfoService {
    @Autowired
    private PtypeDao ptypeDao;

    @Override
    public HttpResult uploadPtypeInfo(String userCode) {
        PtypeDto ptypeDto = ptypeDao.getPtypeInfo(userCode);
        if (ptypeDto == null) {
            return new HttpResult("-1", "编号[" + userCode + "]商品信息不存在", null);
        }
        CommonDto commonDto = new CommonDto();
        commonDto.getXml().getContent().getDetail().setMain(ptypeDto);
        T101HeaderBean t101HeaderBean = new T101HeaderBean();
        t101HeaderBean.setGoodsownid(ptypeDto.getGoodsownid());
        commonDto.getXml().setHeader(t101HeaderBean);
        HttpRequestParams httpRequestParams = new HttpRequestParams<>();
        httpRequestParams.setTrackNo(ptypeDto.getGoodsownid());
        httpRequestParams.getData().add(commonDto);
        log.info("上传国控药品基本信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.PTYPE_URL);
        log.info("上传国控药品基本信息返回json:" + JSON.toJSONString(httpResult));
        return httpResult;
    }

    @Override
    public HttpResult uploadPtypeInfoByRec(int rec) {
        PtypeDto ptypeDto = ptypeDao.getPtypeInfoByRec(rec);
        if (ptypeDto == null) {
            return new HttpResult("-1", "编号[" + rec + "]商品信息不存在", null);
        }
        CommonDto commonDto = new CommonDto();
        commonDto.getXml().getContent().getDetail().setMain(ptypeDto);
        T101HeaderBean t101HeaderBean = new T101HeaderBean();
        t101HeaderBean.setGoodsownid(ptypeDto.getGoodsownid());
        commonDto.getXml().setHeader(t101HeaderBean);
        HttpRequestParams httpRequestParams = new HttpRequestParams<>();
        httpRequestParams.setTrackNo(ptypeDto.getGoodsownid());
        httpRequestParams.getData().add(commonDto);
        log.info("上传国控药品基本信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.PTYPE_URL);
        log.info("上传国控药品基本信息返回json:" + JSON.toJSONString(httpResult));
        return httpResult;
    }

    @Autowired
    private BtypeDao btypeDao;

    @Override
    public HttpResult uploadBtypeInfo(String userCode) {
        BtypeDto btypeDto = btypeDao.getBtypeInfo(userCode);
        if (btypeDto == null) {
            return new HttpResult("-1", "编号[" + userCode + "]往来单位信息不存在", null);
        }
        CommonDto commonDto = new CommonDto();
        commonDto.getXml().getContent().getDetail().setMain(btypeDto);
        T102HeaderBean t102HeaderBean = new T102HeaderBean();
        t102HeaderBean.setGcompanyid(btypeDto.getGcompanyid());
        commonDto.getXml().setHeader(t102HeaderBean);
        HttpRequestParams httpRequestParams = new HttpRequestParams<>();
        httpRequestParams.setTrackNo(btypeDto.getGcompanyid());
        httpRequestParams.getData().add(commonDto);
        log.info("上传国控往来单位基本信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.BTYPE_URL);
        log.info("上传国控往来单位基本信息返回json:" + JSON.toJSONString(httpResult));
        this.uploadBtypeAddressInfo(userCode);
        return httpResult;
    }

    @Override
    public HttpResult uploadBtypeInfoByRec(int rec) {
        BtypeDto btypeDto = btypeDao.getBtypeInfoByRec(rec);
        if (btypeDto == null) {
            return new HttpResult("-1", "编号[" + rec + "]往来单位信息不存在", null);
        }
        CommonDto commonDto = new CommonDto();
        commonDto.getXml().getContent().getDetail().setMain(btypeDto);
        T102HeaderBean t102HeaderBean = new T102HeaderBean();
        t102HeaderBean.setGcompanyid(btypeDto.getGcompanyid());
        commonDto.getXml().setHeader(t102HeaderBean);
        HttpRequestParams httpRequestParams = new HttpRequestParams<>();
        httpRequestParams.setTrackNo(btypeDto.getGcompanyid());
        httpRequestParams.getData().add(commonDto);
        log.info("上传国控往来单位基本信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.BTYPE_URL);
        log.info("上传国控往来单位基本信息返回json:" + JSON.toJSONString(httpResult));
        this.uploadBtypeAddressInfoByRec(rec);
        return httpResult;
    }

    @Override
    public HttpResult uploadBtypeAddressInfo(String userCode) {
        BtypeAddressDto btypeAddressDto = btypeDao.getBtypeAddressInfo(userCode);
        if (btypeAddressDto == null) {
            return new HttpResult("-1", "编号[" + userCode + "]往来单位信息不存在", null);
        }
        CommonDto commonDto = new CommonDto();
        commonDto.getXml().getContent().getDetail().setMain(btypeAddressDto);
        T103HeaderBean t103HeaderBean = new T103HeaderBean();
        t103HeaderBean.setGcompanyid(btypeAddressDto.getGcompanyid());
        commonDto.getXml().setHeader(t103HeaderBean);
        HttpRequestParams httpRequestParams = new HttpRequestParams<>();
        httpRequestParams.getData().add(commonDto);
        log.info("上传国控往来单位地址档案基本信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.BTYPEADDRESS_URL);
        log.info("上传国控往来单位地址档案基本信息返回json:" + JSON.toJSONString(httpResult));
        return httpResult;
    }

    @Override
    public HttpResult uploadBtypeAddressInfoByRec(int rec) {
        BtypeAddressDto btypeAddressDto = btypeDao.getBtypeAddressInfoByRec(rec);
        if (btypeAddressDto == null) {
            return new HttpResult("-1", "编号[" + rec + "]往来单位信息不存在", null);
        }
        CommonDto commonDto = new CommonDto();
        commonDto.getXml().getContent().getDetail().setMain(btypeAddressDto);
        T103HeaderBean t103HeaderBean = new T103HeaderBean();
        t103HeaderBean.setGcompanyid(btypeAddressDto.getGcompanyid());
        commonDto.getXml().setHeader(t103HeaderBean);
        HttpRequestParams httpRequestParams = new HttpRequestParams<>();
        httpRequestParams.getData().add(commonDto);
        log.info("上传国控往来单位地址档案基本信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.BTYPEADDRESS_URL);
        log.info("上传国控往来单位地址档案基本信息返回json:" + JSON.toJSONString(httpResult));
        return httpResult;
    }
}
